package IO操作;

import java.io.*;
import java.util.Arrays;

/**
 * 使用对象流进行对象序列化操作
 *将一个对象转换为一组字节的过程，称为对象序列化，
 * 目的是：要么用于保存或者网络传输
 * 反之，字节转对象，称为反序列化
 * //transient修饰的成员变量会在对象序列化的时候忽略，节省字节，在反序列的时候被忽略的变量不会有数值
 */

public class OOSDemo  {
    public static void main(String[] args) throws IOException {
        String name="刘桑";
        int age=55;
        String gender="男";
        String[] otherInfo={"niubiniubi"};
        Person p =new Person(name,age,gender,otherInfo);

        FileOutputStream fos =new FileOutputStream("person.obj");

        //将一个对象的信息转换为字节然后通过文件流保存到硬盘上
        //对象流：
        ObjectOutputStream oos =new ObjectOutputStream(fos);
        //直接用方法将对象序列化成一组字节，然后写出
        oos.writeObject(p);
        oos.close();
        System.out.println("完毕");



    }

}

class Person implements Serializable{
    private String name;
    private int age;
    private String gender;
    //transient修饰的变量会在对象序列化的时候忽略，节省字节
    private transient String[] otherInfo;

    public Person(){}

    public Person(String name, int age, String gender, String[] otherInfo) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.otherInfo = otherInfo;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", otherInfo=" + Arrays.toString(otherInfo) +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String[] getOtherInfo() {
        return otherInfo;
    }

    public void setOtherInfo(String[] otherInfo) {
        this.otherInfo = otherInfo;
    }
}
